
/****************************************************************************
**
** Copyright (C) 2004-2008 VATATA.com . All rights reserved.
**
** This file is the part of the Miniutil Poject of VATATA.com.
** Vatata's miniutil library is lightweight and portable (for linux
** and Windows). The C++ Class Library included Threadpool, Dll danymic loading, 
** General socket application model and tcp server, httpserver/servlet, etc. 
** It is a pratical utility disigned for who don't want learn the Big C++ 
** Developing Platform, such as  Boost or ACE.
**
** This file may be used under the terms of the GNU General Public
** License versions 2.0 or 3.0 as published by the Free Software
** Foundation and appearing in the files LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file.  Alternatively you may (at
** your option) use any later version of the GNU General Public
** License if such license has been publicly approved by VATATA.com.
** (or its successors, if any) and the Miniutil Project Aministrators. 
**
** Please review the following information to ensure GNU General
** Public Licensing requirements will be met:
** http://www.gnu.org/licenses/lgpl.html. If you are unsure which 
** license is appropriate for your use, please review the following 
** information: http://code.google.com/p/miniutil/ or contact 
** http://www.vatata.com.
**
** In addition, as a special exception, VATATA.com, as the sole
** copyright holder for Miniutil Project, grants users of VATATA
** P2P Platform the right to release and package with the related 
** libraries without the source code.
**
** This file is provided "AS IS" with NO WARRANTY OF ANY KIND,
** INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE. vatata reserves all rights not expressly
** granted herein.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
****************************************************************************/


#ifndef __MINIUTIL__HTTP_SERVER_SERVLETS__H__
#define __MINIUTIL__HTTP_SERVER_SERVLETS__H__

#include "miniutil/virtualhost.h"
#include <map>

namespace miniutil
{
	namespace http
	{
		class servletformdata
		{
		public:
			std::string type;
			std::string name;
			std::string value;
		};
		

		class servlet2 
		{
		public:
			virtualhost_manager * p_vhost;
			matched_rule * p_rule;
			inline servlet2() 
			: p_vhost(NULL)
			, p_rule(NULL)
			{}

			virtual ~servlet2(){}
			virtual void config( miniutil::tinyxml::TiXmlElement * ele ) = 0;
			virtual bool doHead( request * req, response * resp ) = 0;
			virtual bool doGet( request * req, response * resp ) = 0;
			virtual bool doPost( request * req, response * resp ) = 0;
			inline virtual bool doMethod(request * req, response * resp ) 
			{
				return false; 
			}

			virtual int HandlePostData(request * req, std::vector<servletformdata>&, std::string filedir = "");
		};
		
		/*
			config xml string
			...
			<config path="c:/file" type="text/plain" />
			...
		*/

		class text_servlet : public servlet2
		{
		public:
			std::string s_path;
			std::string s_type;
		public:
			virtual void config( miniutil::tinyxml::TiXmlElement * ele );
			virtual bool doHead( request * req, response * resp );
			virtual bool doGet( request * req, response * resp );
			virtual bool doPost( request * req, response * resp );
		};


		/*
			config xml string
			...
			<config path="c:/file" strip_prefix="4" >
				<mime suffix=".txt" type="text/plain" />
			</config>
			...
		*/

		class doc_servlet : public text_servlet
		{
			std::map<string, string> mime;
			int strip_prefix_len ;
			int sleep_interval;
			std::string indexfile;
		public:
			virtual void config( miniutil::tinyxml::TiXmlElement * ele );

			virtual bool doHead( request * req, response * resp );
			virtual bool doGet( request * req, response * resp );
		};

		class simple_servlet: public servlet2
		{
		public:
			virtual void config( miniutil::tinyxml::TiXmlElement * ele );
			virtual bool doHead( request * req, response * resp );
			virtual bool doGet( request * req, response * resp ) = 0 ;
			virtual bool doPost( request * req, response * resp );
		};

		class control_servlet: public simple_servlet
		{
		public:
			virtual bool doGet( request * req, response * resp );
		};

		class sysinfo_servlet: public simple_servlet
		{
		public:
			virtual bool doGet( request * req, response * resp );
		};
		
		class reqproxy_servlet : public simple_servlet
		{
			public:
				virtual bool doGet( request * req, response * resp ) ;
		};

        class flvcrossdomain_servlet: public simple_servlet
        {
            public:
                virtual bool doGet( request *req, response *resp );
        };

    }
}

#endif
